package com.huo.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.huo.dao.OrderSettingDao;
import com.huo.pojo.OrderSetting;
import com.huo.service.OrderSettingService;
import com.huo.utils.POIUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.*;

import static cn.hutool.core.util.NumberUtil.add;

@Service(interfaceClass = OrderSettingService.class)
@Transactional
public class
OrderSettingServiceImpl implements OrderSettingService {

    @Autowired
    private OrderSettingDao orderSettingDao;

    /**
     * @Description: 上传文件/批量导入预约设置数据
     * @Author: 汪青松
     * @Param: [excelFile]
     * @CreateDate: 2021/1/29 21:57
     */
    @Override
    public void add(List<OrderSetting> list) {
        if (list != null && list.size() > 0) {
            for (OrderSetting orderSetting : list) {
                //判断当前日期是否已经进行了预约设置
                long countByOrderDate = orderSettingDao.findCountByOrderDate(orderSetting.getOrderDate());
                if (countByOrderDate > 0) {
                    //已经进行了预约设置，执行更新操作
                    orderSettingDao.editNumberByOrderDate(orderSetting);
                } else {
                    //没有进行预约设置，执行插入操作
                    orderSettingDao.add(orderSetting);
                }
            }
        }
    }

    /**
     * @Description: 根据月份查找预约设置列表
     * @Author: 汪青松
     * @Param: [date]
     * @Return: java.util.List<com.huo.pojo.OrderSetting>
     * @CreateDate: 2021/2/1 22:28
     */
    @Override
    public List<Map> getOrderSettingByMonth(String date) {   //例如：1999-3
        String startDate = date + "-" + "1";
        String endDate = date + "-" + "31'";
        Map<String, String> map = new HashMap<>();
        map.put("startDate", startDate);
        map.put("endDate", endDate);
        List<OrderSetting> orderSettingList = orderSettingDao.getOrderSettingByDate(map);
        List<Map> mapList = new ArrayList<>();

        for (OrderSetting orderSetting : orderSettingList) {
            Map<String, Object> orderSettingMap = new HashMap<>();
            orderSettingMap.put("date", orderSetting.getOrderDate());
            orderSettingMap.put("number", orderSetting.getNumber());
            orderSettingMap.put("reservations", orderSetting.getReservations());

            mapList.add(orderSettingMap);
        }
        return mapList;
    }

    /**
     * @Description: 根据指定日期修改可预约人数
     * @Author: 汪青松
     * @Param: [orderSetting]
     * @Return: void
     * @CreateDate: 2021/2/2 18:38
     */
    @Override
    public void editNumberByDate(OrderSetting orderSetting) {
        long count = orderSettingDao.findCountByOrderDate(orderSetting.getOrderDate());
        if (count > 0) {
            orderSettingDao.editNumberByOrderDate(orderSetting);
        } else {
            orderSettingDao.add(orderSetting);
        }
    }
}
